
import { ref } from "vue";
import { useRouter } from "vue-router";

export const useCommon = () => {
    const router = useRouter();

    const gotopage = (url: string) => {
        router.push(url)
    }

    const toTen = (value: number) => {
        return value < 10 ? "0" + value : value;
    };
    const timeFormat = (value: any, flag: any) => {
        if (!value) return "";
        var str = "-";
        function toten(value: any) {
            return value < 10 ? "0" + value : value;
        }
        var time = new Date(value);
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var day = time.getDate();
        var hour = time.getHours();
        var min = time.getMinutes();
        var sec = time.getSeconds();

        return flag ? year + str + toten(month) + str + toten(day) : year + str + toten(month) + str + toten(day) + "" + toten(hour) + ":" + toten(min) + ":" + toten(sec);
    };
    return {
        gotopage,
        toTen,
        timeFormat
    }
}

export const useDebounceFn = (func: any, interval: any) => {
    let timer: any = null;
    return function () {
        timer && clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(this, arguments);
            timer = null;
        }, interval);
    }
}

export const useThrottleFn = (func: any, interval: any) => {
    let sign: boolean = true;
    return function () {
        if (!sign) return;
        sign = false;
        setTimeout(() => {
            func.apply(this, arguments);
            sign = true;
        }, interval);
    }
}

export const useToggle = (toggle: boolean = true) => {
    const state = ref(true)
    const todochange = (payload: boolean = toggle) => {
        state.value = payload;
    }

    return [
        state,
        todochange
    ]
}

export const useCookies = () => {
    return {}
}